import {
  DelegateTeamGetPage,
  DelegateTeamSave,
  DelegateTeamDelete
} from "@/api/teacher/delegateTeam";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, onMounted } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { GetAllClass } from "@/api/teacher/zsclass";

export function useGroup() {
  const form = reactive({
    DepartmentId: ""
  });

  const updateGroupDto = reactive({
    GroupId: "",
    DepartmentId: "",
    GroupName: "",
    School: "",
    Adviser: "",
    Remark: ""
  });

  const departmentList = ref([]);
  const dataList = ref([]);
  const loading = ref(false);
  const updatePanelVisible = ref(false);

  const rules = {
    GroupName: [
      {
        required: true,
        message: "请输入团队名称",
        trigger: ["blur", "change"]
      }
    ],

    Adviser: [
      {
        required: true,
        message: "请输入学校名称",
        trigger: ["blur", "change"]
      }
    ],
    School: [
      {
        required: true,
        message: "请输入指导老师",
        trigger: ["blur", "change"]
      }
    ]
  };

  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });

  const columns: TableColumnList = [
    {
      label: "序号",
      prop: "GroupId",
      minWidth: 30
    },
    {
      label: "团队名称",
      prop: "GroupName",
      minWidth: 100
    },
    {
      label: "所在学校",
      prop: "Adviser",
      minWidth: 100
    },
    {
      label: "指导老师",
      prop: "School",
      minWidth: 100
    },
    {
      label: "备注",
      prop: "Remark",
      minWidth: 50
    },
    {
      label: "创建时间",
      prop: "CDate",
      minWidth: 50
    },
    {
      label: "操作",
      fixed: "right",
      width: 150,
      slot: "operation"
    }
  ];

  async function onSearch(form) {
    loading.value = true;
    const { data } = await DelegateTeamGetPage({
      pageindex: pagination.currentPage,
      pagesize: pagination.pageSize,
      ...form
    });

    dataList.value = data.list;
    pagination.total = data.totalCount;
    loading.value = false;
  }

  const addGroup = (GroupToRef: FormInstance | undefined) => {
    (GroupToRef as any).validate(async valid => {
      if (!valid) return;
      const respone = await DelegateTeamSave(updateGroupDto);
      console.log(updateGroupDto);
      if (respone.success) {
        ElMessage({
          message: `修改成功`,
          type: "success"
        });
        ClosePanelVisible(GroupToRef);
        onSearch(form);
      } else {
        ElMessage({
          message: `修改失败`,
          type: "error"
        });
      }
    });
  };

  // 修改分组
  async function updateGroup(row: any | undefined) {
    updateGroupDto.GroupId = row.GroupId;
    updateGroupDto.DepartmentId = row.DepartmentId;
    updateGroupDto.Adviser = row.Adviser;
    updateGroupDto.GroupName = row.GroupName;
    updateGroupDto.Remark = row.Remark;
    updateGroupDto.School = row.School;
    updatePanelVisible.value = true;
  }

  //获取班级
  async function InitDepartment() {
    const respone = await GetAllClass();
    if (respone.success) {
      departmentList.value = respone.data;
    }
  }

  // 删除
  async function handleDelete(row) {
    await DelegateTeamDelete({ id: row.GroupId });
    ElMessage({
      message: "删除成功",
      type: "success"
    });
    onSearch(form);
  }

  // 关闭
  function ClosePanelVisible(row: FormInstance | undefined) {
    if (!row) return;
    updatePanelVisible.value = false;
    row.resetFields();
  }

  onMounted(() => {
    InitDepartment();
  });

  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
    onSearch(form);
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
    onSearch(form);
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
    onSearch(form);
  }

  return {
    form,
    rules,
    loading,
    columns,
    dataList,
    departmentList,
    pagination,
    updateGroupDto,
    updatePanelVisible,
    onSearch,
    updateGroup,
    handleDelete,
    ClosePanelVisible,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange,
    addGroup
  };
}
